package com.teemor.common.idempotentspringbootstarter.handler;

import com.teemor.common.idempotentspringbootstarter.cache.KeyCache;
import com.teemor.common.idempotentspringbootstarter.fallback.CallFallback;
import com.teemor.common.idempotentspringbootstarter.valid.KeyValidtor;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description:
 * @author zhoulk
 * @date: 2020/5/13
 */
public class DefualtMethodHandler implements MethodHandler {

    private KeyCache keyCache;
    private KeyValidtor keyValidtor;
    private CallFallback callFallback;
    private List<FieldMapping> fieldMappings;
    private long cacheTime;
    private String keyPre;


    public DefualtMethodHandler(KeyCache keyCache, KeyValidtor keyValidtor, CallFallback callFallback, List<FieldMapping> fieldMappings, String keyPre) {
        this.keyCache = keyCache;
        this.keyValidtor = keyValidtor;
        this.callFallback = callFallback;
        this.fieldMappings = fieldMappings;
        this.keyPre = keyPre;
    }

    @Override
    public Object invoke(Object[] args) {

        StringBuilder key = new StringBuilder();
        key.append("KEY-CACHE-");
        key.append(keyPre).append("-");
        for (int i =0; i<fieldMappings.size(); i++) {
            FieldMapping  fieldMapping = fieldMappings.get(i);
            fieldMapping.getCacheKey(args[i]);
            key.append(fieldMappings);
        }
        keyCache.cacheKey(key.toString(), cacheTime);
        return null;
    }
}
